﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Cocbook_DTO;
using Cocbook_DAL;
using Excel = Microsoft.Office.Interop.Excel;

namespace Sale_and_manage
{
    public partial class frmCustomerReport : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmCustomerReport()
        {
            InitializeComponent();
        }
        private void btnGetFilelocation_Click(object sender, EventArgs e)
        {

        }

        public void ExportFileExcel(string FileLocation)
        {
            Excel.Application Excelapp = new Excel.Application();
            Excel.Workbook oWorkbook;
            System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            oWorkbook = Excelapp.Workbooks.Open(@"D:\My Project\Manage-and-salebook2\Sale and manage\Sale and manage\bin\Debug\CustomerReport.xls", 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

            Excel.Sheets oSheet = oWorkbook.Sheets;
            Excel.Worksheet oWorksheet = (Excel.Worksheet)oSheet.get_Item(1);
            for (int i = 0; i < dgvCustomerReport.RowCount; i++)
            {
                for (int j = 1; j < dgvCustomerReport.ColumnCount; j++)
                {
                    oWorksheet.Cells[2 + i, j] = dgvCustomerReport.Rows[i].Cells[j].Value.ToString();
                }
            }
            System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
            oWorksheet.SaveAs(FileLocation);
            Excelapp.Quit();
            MessageBox.Show("Đã xuất file");
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Title = "Save as...";
            saveFileDialog1.FileName = "Cocbook - Danh sách khách hàng";
            saveFileDialog1.Filter = "Excel 93-2003 Workbook|*.xls";
            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                btnExport.Enabled = false;
                btnRefresh.Enabled = false;
                ExportFileExcel(saveFileDialog1.FileName);
                btnExport.Enabled = true;
                btnRefresh.Enabled = true;
            }
        }

        public void LoadDatatable()
        {
            CustomerDAL cDAL = new CustomerDAL();
            List<CustomerView> lc = cDAL.GetAllCustomerToCV();
            dgvCustomerReport.DataSource = lc;
            dgvCustomerReport.Columns[0].Visible = false;
            dgvCustomerReport.Columns[1].Width = 67;
            dgvCustomerReport.Columns[2].Width = 150;
            dgvCustomerReport.Columns[3].Width = 67;
            dgvCustomerReport.Columns[4].Width = 330;
            dgvCustomerReport.Columns[5].Width = 120;
            dgvCustomerReport.Columns[6].Width = 77;
            dgvCustomerReport.Columns[7].Width = 77;
            dgvCustomerReport.Columns[8].Width = 70;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadDatatable();
        }

        private void frmBookReport_Load(object sender, EventArgs e)
        {
            LoadDatatable();
        }
    }
}